package chixing.day14.work.d4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SumCost {

    public static void main(String[] args) {
        List<Product> list=new ArrayList<>();

        Product p1 = new Product("宝洁","洗手粉",18.5);
        Product p2 = new Product("联合利华","肥皂",4.5);
        Product p3 = new Product("宝洁","牙膏",32.5);
        Product p4 = new Product("宝洁","毛巾",14.5);
        Product p5 = new Product("洁利","洗面奶",26.0);
        Product p6 = new Product("好迪","洗发水",27.5);
        Product p7 = new Product("多芬","沐浴露",38.5);
        Product p8 = new Product("宝洁","洗洁精",3.4);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);
        // 统计各品牌总费用
        Map<String, Double> brandTotalCost = calculateBrandCost(list);

        // 输出结果
        System.out.println("各品牌总费用统计：");
        for (Map. Entry<String, Double> entry : brandTotalCost.entrySet()) {
            System.out.println(entry.getKey() + "：" + entry.getValue() + "元");
        }
    }
    public static Map<String, Double> calculateBrandCost(List<Product> productList) {
        Map<String, Double> brandCostMap = new HashMap<>();

        for (Product product : productList) {
            String brand = product.getBrand();
            double cost = product.getCost();

            // 如果品牌已存在，累加费用；否则，添加新条目
            if (brandCostMap.containsKey(brand)) {
                brandCostMap.put(brand, brandCostMap.get(brand) + cost);
            } else {
                brandCostMap.put(brand, cost);
            }
        }

        return brandCostMap;
    }
}
